with ($ns("g"))
{
    g.AccountClass = function()
    {
        var me = new EventDispatcher();
        
        me.isAuthenticated = false;
        me.user = null;
        me.authCode = null;
        me.token = null;
        
        me.onauthenticated = null;
        
        me.init = function()
        {
            
        };
        
        
        me.login = function(p_options)
        {
            me.isAuthenticated = false;
            me.authCode = null;
            me.token = null;
            console.log("Signing into Google...");
            $.ajax({
                url: $mappath("$google/accounts/ClientLogin"),
                type: "POST",
                data: {
                    accountType: "HOSTED_OR_GOOGLE",
                    service: "reader",
                    Email: p_options.email,
                    Passwd: p_options.password,
                    source: "scroll"
                },
                success: function(p_result)
                {
                    var lines = p_result.split("\n");
                    
                    for (var i = 0; i < lines.length; i++)
                    {
                        var line = lines[i];
                        if (line.startsWith("Auth="))
                        {
                            me.authCode = line.substr(5);
                            me.isAuthenticated = true;
                            console.log("Google account authenticated.");
                            
                            if (typeof(p_options.success) == "function")
                            {
                                p_options.success();
                            }
                            
                            me.trigger("authenticated");
                            
                            _checkToken();

                            g.ReaderService.get({
                                path: "/user-info",
                                success: function(p_result)
                                {
                                    me.user = p_result;
                                },
                                error: function(p_request, p_status, p_error)
                                {
                                    me.user = null;
                                }
                            });
                            return;
                        }
                    }
                },
                error: function(p_request, p_status, p_error)
                {
                    switch (p_request.status)
                    {
                        case 403:
                            // 用户名或密码错误
                            break;
                    }
                    
                    if (typeof(p_options.error) == "function")
                    {
                        p_options.error(p_request, p_status, p_error);
                    }
                }
            });
        };
        
        me.localLogin = function()
        {
            console.log("Signing into Google use LOCAL account...");
            g.Account.isAuthenticated = true;
            g.Account.trigger("authenticated");
        };
        
        
        
        
        
        

        var _tokenTimer = null;
        function _checkToken()
        {
            if (_tokenTimer != null)
            {
                clearTimeout(_tokenTimer);
                _tokenTimer = null;
            }
            g.ReaderService.get({
                path: "/token",
                success: function(p_result)
                {
                    me.token = p_result;
                    console.log("Google Account Token updated: " + me.token);
                    _tokenTimer = setTimeout(_checkToken, 10 * 60000);
                },
                error: function(p_result)
                {
                    _tokenTimer = setTimeout(_checkToken, 10 * 60000);
                }
            });
        }
        
        return me;
    };
    
    g.Account = new g.AccountClass();
    g.Account.init();
}